// 数据
let stuData = [
{kahao:'20212812',yonghu:'叶一刀',jieyue:'《红楼梦》',bianhao:'JF2020402',zuozhe:'曹雪芹',jieriqi:'2021-10-10',huanriqi:'2021-10-10'
,chaoqi:0,diushi:'否',daihuan:1
},
{kahao:'20212813',yonghu:'叶二刀',jieyue:'《史记》',bianhao:'JF2020402',zuozhe:'司马迁',jieriqi:'2021-10-10',huanriqi:'2021-10-10'
,chaoqi:0,diushi:'否',daihuan:2
},
{kahao:'20212814',yonghu:'叶三刀',jieyue:'《东周列国志》',bianhao:'JF2020402',zuozhe:'冯梦龙',jieriqi:'2021-10-10',huanriqi:'2021-10-10'
,chaoqi:0,diushi:'否',daihuan:0
},
{kahao:'20212815',yonghu:'叶四刀',jieyue:'《老人与海》',bianhao:'JF2020402',zuozhe:'海明威',jieriqi:'2021-10-10',huanriqi:'2021-10-10'
,chaoqi:0,diushi:'否',daihuan:3
},
{kahao:'20212816',yonghu:'叶五刀',jieyue:'《乌托邦》',bianhao:'JF2020402',zuozhe:'莫尔',jieriqi:'2021-10-10',huanriqi:'2021-10-10'
,chaoqi:0,diushi:'否',daihuan:0
},
{kahao:'20212817',yonghu:'叶六刀',jieyue:'《威尼斯商人》',bianhao:'JF2020402',zuozhe:'莎士比亚',jieriqi:'2021-10-10',huanriqi:'2021-10-10'
,chaoqi:0,diushi:'否',daihuan:1
},
{kahao:'20212818',yonghu:'叶七刀',jieyue:'《简·爱 》',bianhao:'JF2020402',zuozhe:'夏绿蒂.勃朗特',jieriqi:'2021-10-10',huanriqi:'2021-10-10'
,chaoqi:0,diushi:'否',daihuan:0
},
{kahao:'20212819',yonghu:'叶八刀',jieyue:'《唐璜》',bianhao:'JF2020402',zuozhe:'拜伦',jieriqi:'2021-10-10',huanriqi:'2021-10-10'
,chaoqi:0,diushi:'否',daihuan:2
},
{kahao:'20212820',yonghu:'叶九刀',jieyue:'《三国演义》',bianhao:'JF2020402',zuozhe:'罗贯中',jieriqi:'2021-10-10',huanriqi:'2021-10-10'
,chaoqi:0,diushi:'否',daihuan:1
},
{kahao:'20212821',yonghu:'叶十刀',jieyue:'《呼啸山庄》',bianhao:'JF2020402',zuozhe:'艾米莉.勃朗特',jieriqi:'2021-10-10',huanriqi:'2021-10-10'
,chaoqi:0,diushi:'否',daihuan:1
},
{kahao:'20212822',yonghu:'叶一刀',jieyue:'《鲁宾逊飘流记》',bianhao:'JF2020402',zuozhe:'笛福',jieriqi:'2021-10-10',huanriqi:'2021-10-10'
,chaoqi:0,diushi:'否',daihuan:1
},
{kahao:'20212823',yonghu:'叶二刀',jieyue:'《名利场》',bianhao:'JF2020402',zuozhe:'萨克雷',jieriqi:'2021-10-10',huanriqi:'2021-10-10'
,chaoqi:0,diushi:'否',daihuan:1
},
{kahao:'20212824',yonghu:'叶三刀',jieyue:'《傲慢与偏见》',bianhao:'JF2020402',zuozhe:'简.奥斯丁',jieriqi:'2021-10-10',huanriqi:'2021-10-10'
,chaoqi:0,diushi:'否',daihuan:1
},
{kahao:'20212825',yonghu:'叶四刀',jieyue:'《双城记》',bianhao:'JF2020402',zuozhe:'查尔斯.狄更斯',jieriqi:'2021-10-10',huanriqi:'2021-10-10'
,chaoqi:0,diushi:'否',daihuan:1
},
{kahao:'20212826',yonghu:'叶五刀',jieyue:'《红楼梦》',bianhao:'JF2020402',zuozhe:'曹雪芹',jieriqi:'2021-10-10',huanriqi:'2021-10-10'
,chaoqi:0,diushi:'否',daihuan:1
},
{kahao:'20212827',yonghu:'叶六刀',jieyue:'《红楼梦》',bianhao:'JF2020402',zuozhe:'曹雪芹',jieriqi:'2021-10-10',huanriqi:'2021-10-10'
,chaoqi:0,diushi:'否',daihuan:1
},
{kahao:'20212828',yonghu:'叶七刀',jieyue:'《西游记》',bianhao:'JF2020402',zuozhe:'吴承恩',jieriqi:'2021-10-10',huanriqi:'2021-10-10'
,chaoqi:0,diushi:'否',daihuan:1
},
{kahao:'20212829',yonghu:'叶八刀',jieyue:'《红楼梦》',bianhao:'JF2020402',zuozhe:'曹雪芹',jieriqi:'2021-10-10',huanriqi:'2021-10-10'
,chaoqi:0,diushi:'否',daihuan:1
},
{kahao:'20212830',yonghu:'叶九刀',jieyue:'《红楼梦》',bianhao:'JF2020402',zuozhe:'曹雪芹',jieriqi:'2021-10-10',huanriqi:'2021-10-10'
,chaoqi:0,diushi:'否',daihuan:1
},
{kahao:'20212831',yonghu:'叶十刀',jieyue:'《红楼梦》',bianhao:'JF2020402',zuozhe:'曹雪芹',jieriqi:'2021-10-10',huanriqi:'2021-10-10'
,chaoqi:0,diushi:'否',daihuan:1
},
{kahao:'20212832',yonghu:'叶一刀',jieyue:'《红楼梦》',bianhao:'JF2020402',zuozhe:'曹雪芹',jieriqi:'2021-10-10',huanriqi:'2021-10-10'
,chaoqi:0,diushi:'否',daihuan:1
},
{kahao:'20212833',yonghu:'叶二刀',jieyue:'《红楼梦》',bianhao:'JF2020402',zuozhe:'曹雪芹',jieriqi:'2021-10-10',huanriqi:'2021-10-10'
,chaoqi:0,diushi:'否',daihuan:1
},
{kahao:'20212834',yonghu:'叶三刀',jieyue:'《红楼梦》',bianhao:'JF2020402',zuozhe:'曹雪芹',jieriqi:'2021-10-10',huanriqi:'2021-10-10'
,chaoqi:0,diushi:'否',daihuan:1
},
{kahao:'20212835',yonghu:'叶四刀',jieyue:'《红楼梦》',bianhao:'JF2020402',zuozhe:'曹雪芹',jieriqi:'2021-10-10',huanriqi:'2021-10-10'
,chaoqi:0,diushi:'否',daihuan:1
},
{kahao:'20212836',yonghu:'叶五刀',jieyue:'《红楼梦》',bianhao:'JF2020402',zuozhe:'曹雪芹',jieriqi:'2021-10-10',huanriqi:'2021-10-10'
,chaoqi:0,diushi:'否',daihuan:1
},
{kahao:'20212837',yonghu:'叶六刀',jieyue:'《红楼梦》',bianhao:'JF2020402',zuozhe:'曹雪芹',jieriqi:'2021-10-10',huanriqi:'2021-10-10'
,chaoqi:0,diushi:'否',daihuan:1
},
{kahao:'20212838',yonghu:'叶七刀',jieyue:'《红楼梦》',bianhao:'JF2020402',zuozhe:'曹雪芹',jieriqi:'2021-10-10',huanriqi:'2021-10-10'
,chaoqi:0,diushi:'否',daihuan:1
},
{kahao:'20212839',yonghu:'叶八刀',jieyue:'《红楼梦》',bianhao:'JF2020402',zuozhe:'曹雪芹',jieriqi:'2021-10-10',huanriqi:'2021-10-10'
,chaoqi:0,diushi:'否',daihuan:1
},
{kahao:'20212840',yonghu:'叶九刀',jieyue:'《红楼梦》',bianhao:'JF2020402',zuozhe:'曹雪芹',jieriqi:'2021-10-10',huanriqi:'2021-10-10'
,chaoqi:0,diushi:'否',daihuan:1
},
{kahao:'20212841',yonghu:'叶十刀',jieyue:'《红楼梦》',bianhao:'JF2020402',zuozhe:'曹雪芹',jieriqi:'2021-10-10',huanriqi:'2021-10-10'
,chaoqi:0,diushi:'否',daihuan:1
},
{kahao:'20212842',yonghu:'叶一刀',jieyue:'《红楼梦》',bianhao:'JF2020402',zuozhe:'曹雪芹',jieriqi:'2021-10-10',huanriqi:'2021-10-10'
,chaoqi:0,diushi:'否',daihuan:1
},
{kahao:'20212843',yonghu:'叶二刀',jieyue:'《红楼梦》',bianhao:'JF2020402',zuozhe:'曹雪芹',jieriqi:'2021-10-10',huanriqi:'2021-10-10'
,chaoqi:0,diushi:'否',daihuan:1
},
{kahao:'20212844',yonghu:'叶三刀',jieyue:'《红楼梦》',bianhao:'JF2020402',zuozhe:'曹雪芹',jieriqi:'2021-10-10',huanriqi:'2021-10-10'
,chaoqi:0,diushi:'否',daihuan:1
},
{kahao:'20212845',yonghu:'叶四刀',jieyue:'《红楼梦》',bianhao:'JF2020402',zuozhe:'曹雪芹',jieriqi:'2021-10-10',huanriqi:'2021-10-10'
,chaoqi:0,diushi:'否',daihuan:1
},
{kahao:'20212846',yonghu:'叶五刀',jieyue:'《红楼梦》',bianhao:'JF2020402',zuozhe:'曹雪芹',jieriqi:'2021-10-10',huanriqi:'2021-10-10'
,chaoqi:0,diushi:'否',daihuan:1
},
{kahao:'20212847',yonghu:'叶六刀',jieyue:'《红楼梦》',bianhao:'JF2020402',zuozhe:'曹雪芹',jieriqi:'2021-10-10',huanriqi:'2021-10-10'
,chaoqi:0,diushi:'否',daihuan:1
},
{kahao:'20212848',yonghu:'叶七刀',jieyue:'《红楼梦》',bianhao:'JF2020402',zuozhe:'曹雪芹',jieriqi:'2021-10-10',huanriqi:'2021-10-10'
,chaoqi:0,diushi:'否',daihuan:1
},
{kahao:'20212849',yonghu:'叶八刀',jieyue:'《红楼梦》',bianhao:'JF2020402',zuozhe:'曹雪芹',jieriqi:'2021-10-10',huanriqi:'2021-10-10'
,chaoqi:0,diushi:'否',daihuan:1
},
{kahao:'20212850',yonghu:'叶九刀',jieyue:'《红楼梦》',bianhao:'JF2020402',zuozhe:'曹雪芹',jieriqi:'2021-10-10',huanriqi:'2021-10-10'
,chaoqi:0,diushi:'否',daihuan:1
},
{kahao:'20212851',yonghu:'叶十刀',jieyue:'《红楼梦》',bianhao:'JF2020402',zuozhe:'曹雪芹',jieriqi:'2021-10-10',huanriqi:'2021-10-10'
,chaoqi:0,diushi:'否',daihuan:1
},
{kahao:'20212852',yonghu:'叶一刀',jieyue:'《红楼梦》',bianhao:'JF2020402',zuozhe:'曹雪芹',jieriqi:'2021-10-10',huanriqi:'2021-10-10'
,chaoqi:0,diushi:'否',daihuan:1
},
{kahao:'20212853',yonghu:'叶二刀',jieyue:'《红楼梦》',bianhao:'JF2020402',zuozhe:'曹雪芹',jieriqi:'2021-10-10',huanriqi:'2021-10-10'
,chaoqi:0,diushi:'否',daihuan:1
},
{kahao:'20212854',yonghu:'叶三刀',jieyue:'《红楼梦》',bianhao:'JF2020402',zuozhe:'曹雪芹',jieriqi:'2021-10-10',huanriqi:'2021-10-10'
,chaoqi:0,diushi:'否',daihuan:1
},
{kahao:'20212855',yonghu:'叶四刀',jieyue:'《红楼梦》',bianhao:'JF2020402',zuozhe:'曹雪芹',jieriqi:'2021-10-10',huanriqi:'2021-10-10'
,chaoqi:0,diushi:'否',daihuan:1
},
{kahao:'20212856',yonghu:'叶五刀',jieyue:'《红楼梦》',bianhao:'JF2020402',zuozhe:'曹雪芹',jieriqi:'2021-10-10',huanriqi:'2021-10-10'
,chaoqi:0,diushi:'否',daihuan:1
},

]   
let fenyema=8
let max_page
let pageData = stuData
let dangqianye = 1
let change_num
let del_num
// 用数据渲染表格
function showTable(data) {
    $('#mytable tbody').html('')
    for (let i = 0 ; i < data.length ; i++) {
        $('#mytable tbody').append (
            `<tr>
                <td><input type="checkbox" name="ck" ></td>
                <td>${data[i].kahao}</td>
                <td>${data[i].yonghu}</td>
                <td>${data[i].jieyue}</td>
                <td>${data[i].bianhao}</td>
                <td>${data[i].zuozhe}</td>
                <td>${data[i].jieriqi}</td>
                <td>${data[i].huanriqi}</td>
                <td>${data[i].chaoqi}</td>
                <td>${data[i].diushi}</td>
                <td>${data[i].daihuan}</td>
                <td>
                  <button type="button" data-toggle="modal" data-target="#jyjl_xiugaijyjl" onclick='bianji_jieyue(${i})'>编辑</button>
                  <span> | </span>  
                  <button type="button" data-toggle="modal" data-target="#jyjl_deljyjl" onclick='l_shanchu(${i})'>删除</button>
                </td>    
            </tr>`
        )
    }
}
// 页码分页   8页/10页/20页
function bameiye(){
  
    fenyema=8
    show(1)
}

function shimeiye(){
  
    fenyema=10
    show(1)
}

function ermeiye(){
  
    fenyema=20
    show(1)
}



// 分页
function fenye(num,data) {
    let qishi= (num - 1) * fenyema
    let page_data = data.slice(qishi , qishi + fenyema)
    dangqianye = num
    pageStyle(num)
    showTable(page_data)
}
function pageStyle(num) {
    $('.yema_all').css({'background': 'white','color':'#666666'})
    $('.yema_all').eq(num).css({'background':'rgb(64,158,255)','color':'white'})
}


function showPage(data) {
    $('#jyjl_page').html('')
    max_page = Math.ceil(data.length / fenyema)
    $('#jyjl_page').append(`<div class='yema_all' onclick='page_pre()'> < </div>`)
    for (let i = 1 ; i <= max_page ; i++) {
        $('#jyjl_page').append(`<div class='yema_all'>${i}</div>`)
    }
    $('#jyjl_page').append(
        `<div class='yema_all' onclick='page_next()'> > </div>
        <div id='pageNum' class="dropup">
        <div class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu" data-toggle="dropdown">
        8条/页
        <span class="caret"></span>
        </div>
        <ul class="dropdown-menu" aria-labelledby="dropdownMenu">
            <li class='menuPage' value="8" onclick='bameiye()'>8条/页</li>
            <li class='menuPage' value="10" onclick='shimeiye()'>10条/页</li>
            <li class='menuPage' value="20" onclick='ermeiye()'>20条/页</li>
        </ul>
    </div>`
    )
}
// 上一页
function page_pre() {
    if (dangqianye > 1) {
      dangqianye--
        show(dangqianye)
        return
    } 
    show(dangqianye)
}
// 下一页
function page_next() {
    if (dangqianye < max_page) {
      dangqianye++
        show(dangqianye)
        return
    } 
    show(dangqianye)
}


function l_shanchu(num) {
del_num = num + (dangqianye - 1) * fenyema
}


function jyjishanchu() {
pageData.splice(del_num,1)
show(dangqianye)
}
// 编辑删除
function bianji_jieyue(num) {
change_num = num + (dangqianye - 1) * fenyema
let change_obj = pageData[change_num]
let kahao = document.getElementById('jyjl_xiugai1')
let yonghu = document.getElementById('jyjl_xiugai2')
let jieyue = document.getElementById('jyjl_xiugai3')
let bianhao = document.getElementById('jyjl_xiugai4')
let zuozhe = document.getElementById('jyjl_xiugai5')
let jieriqi = document.getElementById('jyjl_xiugai6')
let huanriqi = document.getElementById('jyjl_xiugai7')
let chaoqi = document.getElementById('jyjl_xiugai8')
let diushi = document.getElementById('jyjl_xiugai9')
let daihuan = document.getElementById('jyjl_xiugai10')
kahao.value = change_obj.kahao
yonghu.value = change_obj.yonghu
jieyue.value = change_obj.jieyue
bianhao.value = change_obj.bianhao
zuozhe.value = change_obj.zuozhe
jieriqi.value = change_obj.jieriqi
huanriqi.value = change_obj.huanriqi
chaoqi.value = change_obj.chaoqi
diushi.value = change_obj.diushi
daihuan.value = change_obj.daihuan
console.log(diushi.value)
}

// 点击确认更改表格中的内容   创建新数据组合，替换内容，并重新渲染
function  jyjl_queren(){
let kahao = document.getElementById('jyjl_xiugai1').value
let yonghu = document.getElementById('jyjl_xiugai2').value
let jieyue = document.getElementById('jyjl_xiugai3').value
let bianhao = document.getElementById('jyjl_xiugai4').value
let zuozhe = document.getElementById('jyjl_xiugai5').value
let jieriqi = document.getElementById('jyjl_xiugai6').value
let huanriqi = document.getElementById('jyjl_xiugai7').value
let chaoqi = document.getElementById('jyjl_xiugai8').value
let diushi = document.getElementById('jyjl_xiugai9').value
let daihuan = document.getElementById('jyjl_xiugai10').value
let new_data = {kahao,yonghu,jieyue,bianhao,zuozhe,jieriqi,huanriqi,chaoqi,diushi,daihuan }
pageData.splice(change_num,1,new_data)
show(dangqianye)
}

// 批量删除
function piliangsha(){
    var ck=$("input:checked[name='ck']")
    for(var i=0;i<ck.length;i++){
      ck[i].parentNode.parentNode.remove()
    for(let j=0;j< pageData.length;j++){
        if(pageData[j].kahao==ck.eq(i).parent().next().text()){
          pageData.splice(j,1)
          show(dangqianye)
        }
    }
    }
}


// 复选框选中全部
function cheAll(){
    var cek = $("#abcd")[0].checked;
    var ck = $("input[name='ck']");
    for(var i = 0; i < ck.length; i++){
        ck[i].checked = cek;
    }
}


function chaxun() {
    if ($('#entext').val() != '') {
      resultData = []
      for (let i = 0; i < stuData.length; i++) {
        if (stuData[i].kahao.match($('#entext').val()) ||
        stuData[i].yonghu.match($('#entext').val()) ||
        stuData[i].jieyue.match($('#entext').val()) ||
        stuData[i].bianhao.match($('#entext').val())) {
          resultData.push(stuData[i])
          console.log(resultData)
        }
      }
    } else {
      resultData = stuData
      
    }
    
    dangqianye= 1
    max_Page = Math.ceil(resultData.length / fenyema)
    showPage(max_Page)
    showTable(resultData)
    
  }
 